/**
* 左边菜单
*/ 
<template>
  <el-menu     :collapse="collapsed" collapse-transition unique-opened    class="el-menu-vertical-demo" background-color="#334157" text-color="#fff"  active-text-color="#ffd04b" >
    
    <div class="logobox">
      <img class="logoimg" src="../assets/img/logo.png" alt="">
    </div>
   
    <el-submenu v-for="menu in hasChildrenMenu" :key="menu.menuid" :index="menu.menuname"
   >
      <template slot="title">
        <i class="iconfont" :class="menu.icon"></i>
        <span>{{menu.menuname }}</span>
      </template>
      <el-menu-item-group v-for="chmenu in menu.menus"  :key="chmenu.menuid">
        <el-menu-item 
        :index="chmenu.menuname"
        @click="clickHandle(chmenu)">
          <i class="iconfont" :class="chmenu.icon"></i>
          <span slot="title">{{chmenu.menuname}}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-submenu>
    <el-menu-item
      v-for="item in noChildrenMenu"
      :key="item.name"
      :index="item.menuname"
      @click="clickHandle(item)"
    >
      <i  class="iconfont" :class="item.icon"></i>
      <span slot="title">{{ item.menuname }}</span>
    </el-menu-item>
  </el-menu>
</template>
<script>
import { menu } from '../api/userMG'
export default {
  name: 'leftnav',
  data() {
    return {
      collapsed: false,
      allmenu: [],
      activeIndex:'1'
    }
  },
  methods:{

    handleOpen(key, keyPath) {
        // console.log(key, keyPath);
      },
      clickHandle(item){
      //  console.log(item)
     
    
     
      if(this.$router.currentRoute.path!=item.url){
        this.$router.push(item.url)
      }
    }
  },
  computed: {
    // 有子菜单
    hasChildrenMenu() {
      return this.allmenu.filter((it) => it.menus);
    },
    // 没有子菜单
    noChildrenMenu() {
      return this.allmenu.filter((it) => !it.menus);
    },
  },
  // 创建完毕状态(里面是操作)
  created() {
    // 获取图形验证码
    let res = {
      success: true,
      data: [
        {
          menuid: 1,
          icon: 'li-icon-xiangmuguanli',
          menuname: '用户管理',
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 2,
              icon: 'icon-cat-skuQuery',
              menuname: '用户列表',
              hasThird: 'N',
              url: '/user/userList',
              menus: null
            },
            {
              menuid: 2,
              icon: 'icon-cat-skuQuery',
              menuname: '添加用户',
              hasThird: 'N',
              url: '/user/addUser',
              menus: null
            }
          ]
        },
        {
          menuid: 33,
          icon: 'li-icon-dingdanguanli',
          menuname: '政策法规',
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 34,
              icon: 'icon-order-manage',
              menuname: '政策法规列表',
              hasThird: 'N',
              url: '/policy/policyList',
              menus: null
            },
            {
              menuid: 34,
              icon: 'icon-order-manage',
              menuname: '添加政策法规',
              hasThird: 'N',
              url: '/policy/addPolicy',
              menus: null
            }
          ]
        },{
          menuid: 33,
          icon: 'li-icon-dingdanguanli',
          menuname: '国防知识',
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 34,
              icon: 'icon-order-manage',
              menuname: '国防知识列表',
              hasThird: 'N',
              url: '/national-defense/NDList',
              menus: null
            },
            {
              menuid: 34,
              icon: 'icon-order-manage',
              menuname: '添加国防知识',
              hasThird: 'N',
              url: '/national-defense/NDadd',
              menus: null
            }
          ]
        },
        {
          menuid: 33,
          icon: 'li-icon-dingdanguanli',
          menuname: '通知公告',
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 34,
              icon: 'icon-order-manage',
              menuname: '通知公告列表',
              hasThird: 'N',
              url: '/affiche/afficheList',
              menus: null
            },
            {
              menuid: 34,
              icon: 'icon-order-manage',
              menuname: '添加通知公告',
              hasThird: 'N',
              url: '/affiche/addAffiche',
              menus: null
            }
          ]
        },
        {
          menuid: 128,
          icon: 'li-icon-shangchengxitongtubiaozitihuayuanwenjian91',
          menuname: '退役管理',
          hasThird: 'N',
          url: '/retirement/retirement',
          menus: null
        },{
          menuid: 128,
          icon: 'li-icon-shangchengxitongtubiaozitihuayuanwenjian91',
          menuname: '后勤保障',
          hasThird: 'N',
          url: '/logistics/logistics',
          menus: null
        }
      ],
      msg: 'success'
    }
          this.allmenu = res.data
    
    // menu(localStorage.getItem('logintoken'))
    //   .then(res => {
    //     console.log(JSON.stringify(res))
    //     if (res.success) {
    //       this.allmenu = res.data
    //     } else {
    //       this.$message.error(res.msg)
    //       return false
    //     }
    //   })
    //   .catch(err => {
    //     this.$message.error('菜单加载失败，请稍后再试！')
    //   })
    // 监听
    this.$root.Bus.$on('toggle', value => {
      // console.log(value,'allmenu')
      this.collapsed = !value
    })
  }
}
</script>
<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 240px;
  min-height: 400px;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
  text-align: left;
}
.el-menu-item-group__title {
  padding: 0px;
}
.el-menu-bg {
  background-color: #1f2d3d !important;
}
.el-menu {
  border: none;
}
.logobox {
  height: 40px;
  line-height: 40px;
  color: #9d9d9d;
  font-size: 20px;
  text-align: center;
  padding: 20px 0px;
}
.logoimg {
  height: 40px;
}
</style>